package tichu_project252.Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import tichu_project252.Model.Model;
import tichu_project252.View.GameFrame;
import tichu_project252.View.Player.PlayerPanel;

/**
 * Handles the Play button.
 *
 * <p> <b> Invariants: </b> </p>
 *
 * @author Alexandra Barka, ΑΜ: 2867, login : barka.
 * @author Euaggelia Petauraki AM: 2875 login: petavraki.
 * @version %I%, %G%
 *
 */
public class playHandler extends Controller implements ActionListener {

    /**
     * The playHandler defines the behavior of the "Play" buttons when an action
     * is performed on them.
     *
     * <p><b>Constructor: </b> playHandler(Model m)</p>
     *
     * <p><b>Precondition: </b> There is a model </p> <p><b>Postcondition: </b>
     * constructs a valid controller </p>
     *
     * @see <a
     * href="http://docs.oracle.com/javase/tutorial/java/IandI/super.html">super</a>
     *
     */
    public playHandler(Model m) {
        super(m);
    }

    /**
     * Invoked when an action occurs.
     *
     * <p><b>Precondition: </b> The button is pushed </p> <p><b>Postcondition:
     * </b> The method handlePlay is performed. </p>
     *
     * @see ActionListener
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton temp = (JButton) e.getSource();
        PlayerPanel p = (PlayerPanel) temp.getParent().getParent(); //get player panel
        GameFrame g = (GameFrame) temp.getParent().getParent().getParent().getParent().getParent().getParent().getParent(); //get game frame
        try {
            if (m.handlePlay(p.getPlayer())) {  // if the cards can be played
                p.cleanup();    // remove them from player hand
                p.invalidate();
                p.validate();
                try {
                    g.addtoCenter(m.getTable().getLastDropped()); //and add the played cards in the table
                } catch (IOException ex) {
                    Logger.getLogger(playHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
                //if(m.checkScore()) {
                    g.setScore();
                //}
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(playHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(playHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (LineUnavailableException ex) {
            Logger.getLogger(playHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedAudioFileException ex) {
            Logger.getLogger(playHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        g.enablePlayer(m.getCurrent()); //continue the game
    }
}
